<?php
/**
 * Class Order
 * Desc  订单Model
 * Create on 2021/8/18 15:40
 * Create by wangyafang
 */

namespace app\api\model\xyb;


use addons\xyb\XypHelper;
use think\Db;

class OrderComplaint extends Base
{
    protected $name = 'xyb_order_complaint';


    public static function createOrderComplaint($orders, $content, $picture, $tags)
    {
        $forms = [
            'remarks'       =>'',
            'content'       => $content,
            'picture'       => $picture,
            'tags'          => $tags,
        ];
        $order_complaint = OrderComplaint::where('order_id', $orders->id)->find();
        if($order_complaint && $order_complaint->status == 2){
            $forms['status'] = 3;
            $forms['updatetime'] = time();
            return OrderComplaint::where('order_id', $orders->id)->update($forms);
        }
        $forms['receive_id'] = $orders->receive_id;
        $forms['user_id'] = $orders->user_id;
        $forms['agent_id'] = $orders->agent_id;
        $forms['type'] = $orders->type;
        $forms['order_no'] = $orders->order_no;
        $forms['order_id'] = $orders->id;
        $forms['status'] = 0;
        $forms['createtime'] = time();
        return OrderComplaint::create($forms);
    }

    public static function getOrderComplaintDetails($order_id){
        $fields = 'id,order_id,content,picture,tags,reply_content,reply_time,status,reply_picture,punish_amount,punish_rider,reject_reason';
        $details = OrderComplaint::field($fields)->where('order_id', $order_id)->find();
        if(!$details){
            return [];
        }

        return $details;
    }

    public static function getOrderComplaint($order_id){
        $fields = 'id,order_id,content,picture,tags,reply_content,reply_time,status,reply_picture,punish_amount,punish_rider,reject_reason';
        $details = OrderComplaint::field($fields)->where('order_id', $order_id)->find();
        if(!$details){
            return [];
        }
        $details->picture = XypHelper::stringToArray($details->picture);
        $details->reply_picture = XypHelper::stringToArray($details->reply_picture);
        return $details;
    }

    public static function submitComplaintReply($order, $reply_content, $reply_picture, $status,$reject_reason){
        $OrderComplaint = OrderComplaint::where('order_id', $order->id)->find();
        return OrderComplaint::where('order_id', $order->id)->update([
            'status' => $status,
            'reply_content' => $reply_content,
            'reply_picture' => $reply_picture,
            'reject_reason' => $reject_reason,
            'reply_time' => time(),
        ]);
    }

    public static function orderComplaintPlatform($order_id,$reply_content, $reply_picture,$punish_rider,$punish_amount){
        $OrderComplaint = OrderComplaint::where('order_id', $order_id)->find();

        $reply_picture = XypHelper::xyb_full_url($reply_picture);
        $res = OrderComplaint::where('order_id', $order_id)->update([
            'status' => 4,
            'reply_content' => $reply_content,
            'reply_picture' => $reply_picture,
            'punish_rider'  => $punish_rider,
            'punish_amount' => $punish_amount,
            'reply_time' => time(),
        ]);
        if(!$res){
            return false;
        }
        if($punish_rider == 2){
            if(!Wallets::punishRider($OrderComplaint->receive_id, $punish_amount)){
                return false;
            }
        }
        return true;
    }

}